//
//  PieChartView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/12.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "PieChartView.h"
#import "UILabel+BasicAttributes.h"
#import "PercentModel.h"


@interface WTMShapeLayer : CAShapeLayer
@property (nonatomic,assign)CGFloat startAngle;
@property (nonatomic,assign)CGFloat endAngle;
@property (nonatomic,assign)BOOL    isSelected;
@property (nonatomic,strong)PercentModel *percentModel;
@end
@implementation WTMShapeLayer
@end

@interface PieChartView()
@property (nonatomic,strong)CAShapeLayer *maskLayer;
@end

@implementation PieChartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI
{
    CGPoint layerPoint;
    CGFloat radiu = 0;
    if (iPhone6p) {
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }else if (iPhone6){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if (iPhoneX){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if(iPhone5){
        layerPoint = CGPointMake(80, 150-110/3);
        radiu = 50.0f;
    }else{
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }
    
    _maskLayer = [CAShapeLayer layer];
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithArcCenter:layerPoint radius:radiu startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES];
    _maskLayer.strokeColor = [UIColor whiteColor].CGColor;
    _maskLayer.lineWidth = radiu;
    _maskLayer.fillColor = [UIColor clearColor].CGColor;
    _maskLayer.path = maskPath.CGPath;
    _maskLayer.strokeEnd = 0;
    self.layer.mask = _maskLayer;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}
- (void)loadPieChartWithArr:(NSArray *)arr adnCenter:(CGPoint)centerPoint andColorArr:(NSArray *)colorArr
{
    CGPoint layerPoint;
    CGFloat radiu = 0;
    if (iPhone6p) {
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }else if (iPhone6){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if (iPhoneX){
        layerPoint = CGPointMake(90, 150-110/3);
        radiu = 60.0f;
    }else if(iPhone5){
        layerPoint = CGPointMake(80, 150-110/3);
        radiu = 50.0f;
    }else{
        layerPoint = CGPointMake(100, 150-110/3);
        radiu = 69.0f;
    }
    CGFloat start = -M_PI_2;
    CGFloat end = start;
    while (arr.count > self.layer.sublayers.count) {
        WTMShapeLayer *pieLayer = [WTMShapeLayer layer];
        pieLayer.strokeColor = NULL;
        [self.layer addSublayer:pieLayer];
    }
    for (int i = 0; i < self.layer.sublayers.count; i ++) {
        WTMShapeLayer *pieLayer = (WTMShapeLayer *)self.layer.sublayers[i];
        if (i < arr.count) {
            PercentModel *model = arr[i];
            pieLayer.hidden = NO;
            end =  start + M_PI*2*model.percent ;
            UIBezierPath *piePath = [UIBezierPath bezierPath];
            [piePath moveToPoint:layerPoint];
            [piePath addArcWithCenter:layerPoint radius:radiu startAngle:start endAngle:end clockwise:YES];
            UIColor *color = colorArr[i];
            pieLayer.fillColor = color.CGColor;
            pieLayer.startAngle = start;
            pieLayer.endAngle = end;
            pieLayer.path = piePath.CGPath;
            pieLayer.percentModel = model;
            start = end;
        }else{
            pieLayer.hidden = YES;
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [touches.anyObject locationInView:self];
    [self upDateLayersWithPoint:point];
}
- (void)upDateLayersWithPoint:(CGPoint)point{
    for (WTMShapeLayer *layer in self.layer.sublayers) {
        if (CGPathContainsPoint(layer.path, &CGAffineTransformIdentity, point, 0) && !layer.isSelected) {
            layer.isSelected = YES;
            CGPoint currPos = layer.position;
            double middleAngle = (layer.startAngle + layer.endAngle)/2.0;
            CGPoint newPos = CGPointMake(currPos.x + 5*cos(middleAngle), currPos.y + 5*sin(middleAngle));
            layer.position = newPos;
            if (_layerModel) {
                _layerModel(layer.percentModel);
            }
        }else{
            layer.position = CGPointMake(0, 0);
            layer.isSelected = NO;
        }
    }
}
- (void)stroke{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 1.f;
    animation.fromValue = [NSNumber numberWithFloat:0.f];
    animation.toValue = [NSNumber numberWithFloat:1.f];
    animation.autoreverses = NO;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [_maskLayer addAnimation:animation forKey:@"strokeEnd"];
}
@end
